#include "des.h"
#include "xor.h"
#include "log_api.h"
//#include "user_wwdg.h"
#include "event_app.h"
//#include "basic_app.h"
//#include "sound_app.h"
//#include "gtx31x_app.h"
#include "rfid_board.h"
#include "memroy_app.h"
#include "irq_contorl.h"
#include "protocol_api.h"
#include "protocol_app.h"
#include "real_time_app.h"
#include "cardholder_api.h"
#include "device_parameter.h"

#if _RFID_FUNCTION_ENABLE_

/**
 * @brief MISO 管脚初始化
 *
 */
static void RfidMisoConfiguration(void);

/**
 * @brief NSS 管脚初始化
 *
 */
static void RfidNssConfiguration(void);

/**
 * @brief SCK 管脚初始化
 *
 */
static void RfidSckConfiguration(void);

/**
 * @brief MOSI 管脚初始化
 *
 */
static void RfidMosiConfiguration(void);

/**
 * @brief 复位管脚初始化
 *
 */
static void RfidResetConfiguration(void);

/**
 * @brief 读取 MISO 管脚电平
 *
 * @return bool - 管脚电平状态（true：高电平 false：低电平）
 */
static bool RfidReadMisoPinLevel(void);

/**
 * @brief 设置 MOSI 管脚为高电平
 *
 */
static void RfidSetMosiHighLevel(void);

/**
 * @brief 设置 MOSI 管脚为低电平
 *
 */
static void RfidSetMosiLowLevel(void);

/**
 * @brief 设置 SCK 管脚为高电平
 *
 */
static void RfidSetSckHighLevel(void);

/**
 * @brief 设置 SCK 管脚为低电平
 *
 */
static void RfidSetSckLowLevel(void);

/**
 * @brief 设置 NSS 管脚为高电平
 *
 */
static void RfidSetNssHighLevel(void);

/**
 * @brief 设置 NSS 管脚为低电平
 *
 */
static void RfidSetNssLowLevel(void);

/**
 * @brief 设置复位管脚为高电平
 *
 */
static void RfidSetResetHighLevel(void);

/**
 * @brief 设置复位管脚为低电平
 *
 */
static void RfidSetResetLowLevel(void);

#if _CARD_DETECTION_TYPE_ == _CARD_DETECTION_ADC_
/**
 * @brief 设置检测电压使能管脚为低电平
 *
 */
static void RfidSetDetectionEnableLowLevel(void);

/**
 * @brief 配置采样管脚为 ADC 功能
 *
 */
static void RfidConfiguringDetectionPinAdcFunctions(void);

/**
 * @brief 配置采样管脚为 IO 功能
 *
 */
static void RfidConfiguringDetectionPinIoFunctions(void);

#else
/**
 * @brief 配置采样管教为中断功能
 *
 */
static void RfidConfigurationDetectionPinIrqnFunctions(void);

/**
 * @brief 获取采样管脚电平
 *
 * @return bool - 管脚电平状态（true：高电平 false：低电平）
 */
static bool RfidReadDetectionPinLevel(void);

/**
 * @brief 使能中断
 *
 */
static void RfidEnableIrqn(void);

/**
 * @brief 屏蔽中断
 *
 */
static void RfidDisableIrqn(void);

/**
 * @brief 读取卡片检测管脚中断触发标识
 *
 * @return bool - 中断触发标识（true：触发 false：未触发）
 */
static bool RfidReadCardDetectionItFlagBit(void);

/**
 * @brief 清除卡片检测管脚中断触发标识
 *
 */
void RfidClearCardDetectionITFlagBit(void);

/**
 * @brief 中断回调
 *
 * @return bool - 触发标识（true：触发 false：静默）
 */
static bool RfidTiggerIrqnCallback(void);

/**
 * @brief 事件处理回调
 *
 */
static void RfidEventHandlerCallback(void);

#endif

/**
 * @brief 事件处理中心
 *
 */
static uint16_t RfidEventTaskCallback(uint8_t TaskId, uint16_t Events);

static uint8_t RfidEventTaskId = 0;

static RfidExternFunctionTable_t RfidExternalInterfaces = {0};

#if _CARD_DETECTION_TYPE_ == _CARD_DETECTION_ADC_
static TimedTable_t CardDetectionNode = {0};
#else
static bool RfidIrqTiggerEn = false;
static IrqControl_t RfidIrqContorlNode = {0};
static EventHandlerTable_t RfidIrqEventHandlerNode = {0};
#endif

static void RfidMisoConfiguration(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  GPIOB_ModeCfg(RFID_SPI_MISO_PIN, GPIO_ModeIN_PU);

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  GPIOA_ModeCfg(RFID_SPI_MISO_PIN, GPIO_ModeIN_PU);

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  GPIOA_ModeCfg(RFID_SPI_MISO_PIN, GPIO_ModeIN_PU);

#elif _BOARD_TYPE_ == _9905_B_BOARD_
  GPIOB_ModeCfg(RFID_SPI_MISO_PIN, GPIO_ModeIN_PU);
#endif
}

static void RfidNssConfiguration(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  GPIOB_ModeCfg(RFID_SPI_NSS_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  GPIOA_ModeCfg(RFID_SPI_NSS_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  GPIOA_ModeCfg(RFID_SPI_NSS_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _9905_B_BOARD_
  GPIOB_ModeCfg(RFID_SPI_NSS_PIN, GPIO_ModeOut_PP_5mA);
#endif

  RfidSetNssHighLevel();
}

static void RfidSckConfiguration(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  GPIOB_ModeCfg(RFID_SPI_SCK_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  GPIOA_ModeCfg(RFID_SPI_SCK_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  GPIOA_ModeCfg(RFID_SPI_SCK_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _9905_B_BOARD_
  GPIOB_ModeCfg(RFID_SPI_SCK_PIN, GPIO_ModeOut_PP_5mA);
#endif

  RfidSetSckLowLevel();
}

static void RfidMosiConfiguration(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  GPIOB_ModeCfg(RFID_SPI_MOSI_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  GPIOA_ModeCfg(RFID_SPI_MOSI_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  GPIOA_ModeCfg(RFID_SPI_MOSI_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _9905_B_BOARD_
  GPIOB_ModeCfg(RFID_SPI_MOSI_PIN, GPIO_ModeOut_PP_5mA);
#endif

  RfidSetMosiLowLevel();
}

static void RfidResetConfiguration(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  GPIOB_ModeCfg(RFID_DEVICE_RESET_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  GPIOA_ModeCfg(RFID_DEVICE_RESET_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  GPIOA_ModeCfg(RFID_DEVICE_RESET_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _9905_B_BOARD_
  GPIOB_ModeCfg(RFID_DEVICE_RESET_PIN, GPIO_ModeOut_PP_5mA);
#endif

  RfidSetResetHighLevel();
}

static bool RfidReadMisoPinLevel(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  if (GPIOB_ReadPortPin(RFID_SPI_MISO_PIN) == RFID_SPI_MISO_PIN)
    return true;

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  if (GPIOA_ReadPortPin(RFID_SPI_MISO_PIN) == RFID_SPI_MISO_PIN)
    return true;

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  if (GPIOA_ReadPortPin(RFID_SPI_MISO_PIN) == RFID_SPI_MISO_PIN)
    return true;

#elif _BOARD_TYPE_ == _9905_B_BOARD_
  if (GPIOB_ReadPortPin(RFID_SPI_MISO_PIN) == RFID_SPI_MISO_PIN)
    return true;
#endif

  return false;
}

static void RfidSetMosiHighLevel(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  GPIOB_SetBits(RFID_SPI_MOSI_PIN);

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  GPIOA_SetBits(RFID_SPI_MOSI_PIN);

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  GPIOA_SetBits(RFID_SPI_MOSI_PIN);

#elif _BOARD_TYPE_ == _9905_B_BOARD_
  GPIOB_SetBits(RFID_SPI_MOSI_PIN);
#endif
}

static void RfidSetMosiLowLevel(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  GPIOB_ResetBits(RFID_SPI_MOSI_PIN);

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  GPIOA_ResetBits(RFID_SPI_MOSI_PIN);

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  GPIOA_ResetBits(RFID_SPI_MOSI_PIN);

#elif _BOARD_TYPE_ == _9905_B_BOARD_
  GPIOB_ResetBits(RFID_SPI_MOSI_PIN);
#endif
}

static void RfidSetSckHighLevel(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  GPIOB_SetBits(RFID_SPI_SCK_PIN);

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  GPIOA_SetBits(RFID_SPI_SCK_PIN);

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  GPIOA_SetBits(RFID_SPI_SCK_PIN);

#elif _BOARD_TYPE_ == _9905_B_BOARD_
  GPIOB_SetBits(RFID_SPI_SCK_PIN);
#endif
}

static void RfidSetSckLowLevel(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  GPIOB_ResetBits(RFID_SPI_SCK_PIN);

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  GPIOA_ResetBits(RFID_SPI_SCK_PIN);

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  GPIOA_ResetBits(RFID_SPI_SCK_PIN);

#elif _BOARD_TYPE_ == _9905_B_BOARD_
  GPIOB_ResetBits(RFID_SPI_SCK_PIN);
#endif
}

static void RfidSetNssHighLevel(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  GPIOB_SetBits(RFID_SPI_NSS_PIN);

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  GPIOA_SetBits(RFID_SPI_NSS_PIN);

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  GPIOA_SetBits(RFID_SPI_NSS_PIN);

#elif _BOARD_TYPE_ == _9905_B_BOARD_
  GPIOB_SetBits(RFID_SPI_NSS_PIN);
#endif
}

static void RfidSetNssLowLevel(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  GPIOB_ResetBits(RFID_SPI_NSS_PIN);

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  GPIOA_ResetBits(RFID_SPI_NSS_PIN);

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  GPIOA_ResetBits(RFID_SPI_NSS_PIN);

#elif _BOARD_TYPE_ == _9905_B_BOARD_
  GPIOB_ResetBits(RFID_SPI_NSS_PIN);
#endif
}

static void RfidSetResetHighLevel(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  GPIOB_SetBits(RFID_DEVICE_RESET_PIN);

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  GPIOA_SetBits(RFID_DEVICE_RESET_PIN);

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  GPIOA_SetBits(RFID_DEVICE_RESET_PIN);

#elif _BOARD_TYPE_ == _9905_B_BOARD_
  GPIOB_SetBits(RFID_DEVICE_RESET_PIN);
#endif
}

static void RfidSetResetLowLevel(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  GPIOB_ResetBits(RFID_DEVICE_RESET_PIN);

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  GPIOA_ResetBits(RFID_DEVICE_RESET_PIN);

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  GPIOA_ResetBits(RFID_DEVICE_RESET_PIN);

#elif _BOARD_TYPE_ == _9905_B_BOARD_
  GPIOB_ResetBits(RFID_DEVICE_RESET_PIN);
#endif
}

#if _CARD_DETECTION_TYPE_ == _CARD_DETECTION_ADC_
static void RfidSetDetectionEnableLowLevel(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  GPIOA_ResetBits(RFID_CARD_DETECTION_PIN);

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  GPIOA_ResetBits(RFID_CARD_DETECTION_PIN);

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  GPIOA_ResetBits(RFID_CARD_DETECTION_PIN);

#elif _BOARD_TYPE_ == _9905_B_BOARD_
  GPIOB_ResetBits(RFID_CARD_DETECTION_PIN);
#endif
}

static void RfidConfiguringDetectionPinAdcFunctions(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  GPIOA_ModeCfg(RFID_CARD_DETECTION_PIN, GPIO_ModeIN_Floating);

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  GPIOA_ModeCfg(RFID_CARD_DETECTION_PIN, GPIO_ModeIN_Floating);

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  GPIOA_ModeCfg(RFID_CARD_DETECTION_PIN, GPIO_ModeIN_Floating);
#endif

  ADC_ExtSingleChSampInit(SampleFreq_8, ADC_PGA_0);
  int16_t AdcRoughCalibValue = ADC_DataCalib_Rough();
  ADC_ChannelCfg(RFID_VOLTAGE_DETECTION_CHANNEL);
}

static void RfidConfiguringDetectionPinIoFunctions(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  R8_ADC_CFG &= ~RB_ADC_POWER_ON;
  GPIOA_ModeCfg(RFID_CARD_DETECTION_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  R8_ADC_CFG &= ~RB_ADC_POWER_ON;
  GPIOA_ModeCfg(RFID_CARD_DETECTION_PIN, GPIO_ModeOut_PP_5mA);

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  R8_ADC_CFG &= ~RB_ADC_POWER_ON;
  GPIOA_ModeCfg(RFID_CARD_DETECTION_PIN, GPIO_ModeOut_PP_5mA);
#endif

  RfidSetDetectionEnableLowLevel();
}

#else
static void RfidConfigurationDetectionPinIrqnFunctions(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  GPIOA_ModeCfg(RFID_CARD_DETECTION_PIN, GPIO_ModeIN_PU);

  GPIOA_ITModeCfg(RFID_CARD_DETECTION_PIN, GPIO_ITMode_FallEdge);
  PFIC_EnableIRQ(GPIO_A_IRQn);
  PWR_PeriphWakeUpCfg(ENABLE, RB_SLP_GPIO_WAKE, Long_Delay);

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  GPIOA_ModeCfg(RFID_CARD_DETECTION_PIN, GPIO_ModeIN_PU);

  GPIOA_ITModeCfg(RFID_CARD_DETECTION_PIN, GPIO_ITMode_FallEdge);
  PFIC_EnableIRQ(GPIO_A_IRQn);
  PWR_PeriphWakeUpCfg(ENABLE, RB_SLP_GPIO_WAKE, Long_Delay);

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  GPIOA_ModeCfg(RFID_CARD_DETECTION_PIN, GPIO_ModeIN_Floating);

  GPIOA_ITModeCfg(RFID_CARD_DETECTION_PIN, GPIO_ITMode_FallEdge);
  PFIC_EnableIRQ(GPIO_A_IRQn);
  PWR_PeriphWakeUpCfg(ENABLE, RB_SLP_GPIO_WAKE, Long_Delay);

#elif _BOARD_TYPE_ == _9905_B_BOARD_
  GPIOB_ModeCfg(RFID_CARD_DETECTION_PIN, GPIO_ModeIN_PU);

  GPIOB_ITModeCfg(RFID_CARD_DETECTION_PIN, GPIO_ITMode_FallEdge);
  PFIC_EnableIRQ(GPIO_B_IRQn);
  PWR_PeriphWakeUpCfg(ENABLE, RB_SLP_GPIO_WAKE, Long_Delay);
#endif
}

static bool RfidReadDetectionPinLevel(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  if (GPIOA_ReadPortPin(RFID_CARD_DETECTION_PIN) == RFID_CARD_DETECTION_PIN)

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  if (GPIOA_ReadPortPin(RFID_CARD_DETECTION_PIN) == RFID_CARD_DETECTION_PIN)

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  if (GPIOA_ReadPortPin(RFID_CARD_DETECTION_PIN) == RFID_CARD_DETECTION_PIN)

#elif _BOARD_TYPE_ == _9905_B_BOARD_
  if (GPIOB_ReadPortPin(RFID_CARD_DETECTION_PIN) == RFID_CARD_DETECTION_PIN)
#endif
    return true;

  return false;
}

static bool RfidTiggerIrqnCallback(void)
{
  PRINT("[rfid] : RfidTiggerIrqnCallback\r\n");
  if (RfidReadCardDetectionItFlagBit() == true)
  {
    RfidClearCardDetectionITFlagBit();

    RfidIrqTiggerEn = true;
    return true;
  }

  return false;
}

static void RfidEventHandlerCallback(void)
{
  // PRINT("[rfid] : RfidEventHandlerCallback\r\n");
  if (RfidIrqTiggerEn == true)
  {
    RfidIrqTiggerEn = false;

    RfidDelayMs(5);
    printf("nfc int\r\n");
    if (RfidReadDetectionPinLevel() != true)
    {
       printf("nfc int\r\n");
      if (RfidExternalInterfaces.CardDetectionTask != NULL)
      {
        RfidExternalInterfaces.CardDetectionTask();
      }
    }
  }
}

static void RfidEnableIrqn(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  R16_PA_INT_EN |= RFID_CARD_DETECTION_PIN;
  GPIOA_ClearITFlagBit(RFID_CARD_DETECTION_PIN);

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  R16_PA_INT_EN |= RFID_CARD_DETECTION_PIN;
  GPIOA_ClearITFlagBit(RFID_CARD_DETECTION_PIN);

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  R16_PA_INT_EN |= RFID_CARD_DETECTION_PIN;
  GPIOA_ClearITFlagBit(RFID_CARD_DETECTION_PIN);
#endif
}

static void RfidDisableIrqn(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  R16_PA_INT_EN &= ~RFID_CARD_DETECTION_PIN;
  GPIOA_ClearITFlagBit(RFID_CARD_DETECTION_PIN);

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  R16_PA_INT_EN &= ~RFID_CARD_DETECTION_PIN;
  GPIOA_ClearITFlagBit(RFID_CARD_DETECTION_PIN);

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  R16_PA_INT_EN &= ~RFID_CARD_DETECTION_PIN;
  GPIOA_ClearITFlagBit(RFID_CARD_DETECTION_PIN);
#endif
}

static bool RfidReadCardDetectionItFlagBit(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  if (GPIOA_ReadITFlagBit(RFID_CARD_DETECTION_PIN) == RFID_CARD_DETECTION_PIN)

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  if (GPIOA_ReadITFlagBit(RFID_CARD_DETECTION_PIN) == RFID_CARD_DETECTION_PIN)

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  if (GPIOA_ReadITFlagBit(RFID_CARD_DETECTION_PIN) == RFID_CARD_DETECTION_PIN)

#elif _BOARD_TYPE_ == _9905_B_BOARD_
  if (GPIOB_ReadITFlagBit(RFID_CARD_DETECTION_PIN) == RFID_CARD_DETECTION_PIN)
#endif
    return true;

  return false;
}

void RfidClearCardDetectionITFlagBit(void)
{
#if _BOARD_TYPE_ == _6681_A_BOARD_
  GPIOA_ClearITFlagBit(RFID_CARD_DETECTION_PIN);

#elif _BOARD_TYPE_ == _M157_A_BOARD_
  GPIOA_ClearITFlagBit(RFID_CARD_DETECTION_PIN);

#elif _BOARD_TYPE_ == _9905_A_BOARD_
  GPIOA_ClearITFlagBit(RFID_CARD_DETECTION_PIN);

#elif _BOARD_TYPE_ == _9905_B_BOARD_
  GPIOB_ClearITFlagBit(RFID_CARD_DETECTION_PIN);
#endif
}
#endif

static uint16_t RfidEventTaskCallback(uint8_t TaskId, uint16_t Events)
{
  if (RfidEventTaskId == TaskId)
  {
    if (Events & RFID_SELF_TEST)
    {
      if (RfidExternalInterfaces.SelfTestTask != NULL)
      {
        RfidExternalInterfaces.SelfTestTask();
      }

      return (Events ^ RFID_SELF_TEST);
    }

    if (Events & RFID_READ_CARD_DATA)
    {
      if (RfidExternalInterfaces.ReadCardDataTask != NULL)
      {
        RfidExternalInterfaces.ReadCardDataTask();
      }

      return (Events ^ RFID_READ_CARD_DATA);
    }
  }

  return 0;
}

#if _CARD_DETECTION_TYPE_ == _CARD_DETECTION_ADC_
void RfidCardDetectionConfiguration(bool En)
{
  CardDetectionNode.En = En;
}

void RfidSetCardDetectionIntervalTime(uint8_t Interval)
{
  CardDetectionNode.AccruedTime = 0;
  CardDetectionNode.TriggerTime = Interval;
}

void RfidEnableDetection(void)
{
  RfidSetResetPinLevel(true);
  RfidConfiguringDetectionPinAdcFunctions();
}

void RfidDisableDetection(void)
{
  RfidSetResetPinLevel(false);
  RfidConfiguringDetectionPinIoFunctions();
}

#else
void RfidEnableInterrupt(void)
{
  RfidEnableIrqn();
}

void RfidDisableInterrupt(void)
{
  RfidDisableIrqn();
}
#endif

void RfidDelayUs(uint32_t Us)
{
  DelayUs(Us);
}

void RfidDelayMs(uint32_t Ms)
{
  DelayMs(Ms);
}

bool RfidGetMisoPinLevel(void)
{
  return RfidReadMisoPinLevel();
}

void RfidSetMosiPinLevel(bool PinLevel)
{
  (PinLevel == true) ? RfidSetMosiHighLevel() : RfidSetMosiLowLevel();
}

void RfidSetSckPinLevel(bool PinLevel)
{
  (PinLevel == true) ? RfidSetSckHighLevel() : RfidSetSckLowLevel();
}

void RfidSetNssPinLevel(bool PinLevel)
{
  (PinLevel == true) ? RfidSetNssHighLevel() : RfidSetNssLowLevel();
}

void RfidSetResetPinLevel(bool PinLevel)
{
  (PinLevel == true) ? RfidSetResetHighLevel() : RfidSetResetLowLevel();
}

uint16_t RfidGetInputVoltage(void)
{
  uint16_t SamplingData = ADC_ExcutSingleConver();
  return (uint16_t)((SamplingData * 1000) / 2048);
}

void RfidHardwareReset(void)
{
  RfidSetResetPinLevel(false);
  RfidDelayMs(2);
  RfidSetResetPinLevel(true);
  RfidDelayMs(10);
}

bool RfidGetDeviceActivationStatus(void)
{
  uint8_t WorkMode = GetDeviceInitStatus() & (~FIRMWARE_UPGRADE);
  if (WorkMode == NORMAL_MODE)
    return true;

  return false;
}

bool RfidGetAllowedToOpenDoubleLock(void)
{
  return GetAllowedToOpenDoubleLockEn();
}

bool RfidGetNormalOpenEn(void)
{
  return GetNormalOpenEn();
}

RfidReadCard_t RfidGetReaderCardType(void)
{
  uint8_t ReadCardType = GetReadTheCardType();

  switch (ReadCardType)
  {
  case READER_BY_CPU_CARD:
  case READER_BY_H_WORLD_GROUP_CPU_CARD:
    return STANDBY_CPU_MODE;

  case READER_BY_IDENTIY_CARD:
    return _2RD_GENERATION_ID_CARD;
  }
  return MIFARE_CARD_MODE;
}

RfidCardProtocol_t RfidGetCardReadingProtocol(void)
{
  uint8_t Protocol = GetReadTheCardType();

  switch (Protocol)
  {
  case READER_BY_H_WORLD_GROUP_CPU_CARD:
  case READER_BY_H_WORLD_GROUP_MIFARE_CARD:
    return RFID_CARD_PROTOCOL_HZ;
  }

  return RFID_CARD_PROTOCOL_YG;
}

bool RfidGetValidationStartTimeEnableFlag(void)
{
  return GetStartTimeVerifyEn();
}

uint8_t RfidGetStartSectorNumber(void)
{
  return GetCardOperationSector();
}

void RfidGetSectorAccessKey(uint8_t *pKey)
{
  GetCardOperationSectorKey(pKey);
}

bool RfidSetTimeZone(uint8_t TimeZone)
{
  if (SetSystemTimeZone(TimeZone))
    return true;

  return false;
}

uint8_t RfidGetGardenNumber(void)
{
  return GetLockGardenIdNumber();
}

uint8_t RfidGetBuildNumber(void)
{
  return GetLockBuildingNumber();
}

uint8_t RfidGetFloorNumber(void)
{
  return GetLockFloorNumber();
}

uint32_t RfidGetRoomNumber(void)
{
  return GetLockRoomIdNumber();
}

uint8_t RfidGetChildRommNumber(void)
{
  return GetLockSubroomNumber();
}

uint16_t RfidGetRoomType(void)
{
  return GetRoomType();
}

uint32_t RfidGetGroupingNumber(void)
{
  return GetGroupingNumber();
}

uint16_t RfidGetGuestCardReplaceId(void)
{
  return GetGuestCardReplaceId();
}

void RfidSetGuestCardReplaceId(uint16_t ReplaceId)
{
  SetGuestCardReplaceId(ReplaceId);
}

uint16_t RfidGetMasterCardReplaceId(void)
{
  return GetMasterCardReplaceId();
}

void RfidSetMasterCardReplaceId(uint16_t ReplaceId)
{
  SetMasterCardReplaceId(ReplaceId);
}

uint16_t RfidGetBuildCardReplaceId(void)
{
  return GetBuildCardReplaceId();
}

void RfidSetBuildCardReplaceId(uint16_t ReplaceId)
{
  SetBuildCardReplaceId(ReplaceId);
}

uint16_t RfidGetFloorCardReplaceId(void)
{
  return GetFloorCardReplaceId();
}

void RfidSetFloorCardReplaceId(uint16_t ReplaceId)
{
  SetFloorCardReplaceId(ReplaceId);
}

uint16_t RfidGetParkCardReplaceId(void)
{
  return GetParkCardReplaceId();
}

void RfidSetParkCardReplaceId(uint16_t ReplaceId)
{
  SetParkCardReplaceId(ReplaceId);
}

uint16_t RfidGetNormalOpenCardReplaceId(void)
{
  return GetNormalOpenCardReplaceId();
}

void RfidSetNormalOpenCardReplaceId(uint16_t ReplaceId)
{
  SetNormalOpenCardReplaceId(ReplaceId);
}

uint32_t RfidGetGuestCardReplaceTime(void)
{
  return GetGuestCardReplaceTime();
}

void RfidSetGuestCardReplaceTime(uint32_t Time)
{
  SetGuestCardReplaceTime(Time);
}

uint32_t RfidGetMasterCardReplaceTime(void)
{
  return GetMasterCardReplaceTime();
}

void RfidSetMasterCardReplaceTime(uint32_t Time)
{
  SetMasterCardReplaceTime(Time);
}

uint32_t RfidGetBuildCardReplaceTime(void)
{
  return GetBuildCardReplaceTime();
}

void RfidSetBuildCardReplaceTime(uint32_t Time)
{
  SetBuildCardReplaceTime(Time);
}

uint32_t RfidGetFloorCardReplaceTime(void)
{
  return GetFloorCardReplaceTime();
}

void RfidSetFloorCardReplaceTime(uint32_t Time)
{
  SetFloorCardReplaceTime(Time);
}

uint32_t RfidGetParkCardReplaceTime(void)
{
  return GetParkCardReplaceTime();
}

void RfidSetParkCardReplaceTime(uint32_t Time)
{
  SetParkCardReplaceTime(Time);
}

uint32_t RfidGetNormalOpenCardReplaceTime(void)
{
  return GetNormalOpenCardReplaceTime();
}

void RfidSetNormalOpenCardReplaceTime(uint32_t Time)
{
  SetNormalOpenCardReplaceTime(Time);
}

uint8_t RfidGetAllowableTimeError(void)
{
  return GetAllowableTimeError();
}

void RfidUpdateDeviceParameters(void)
{
  MemoryUpdateDeviceParameters();
}

bool RfidDetermineWhetherItIsInTheNormallyOpenState(void)
{
//  if (GetMotorActionStatus() == MOTOR_NORMALLY_OPEN)
//    return true;

  return false;
}

bool RfidGetDeviceActiveStatus(void)
{
#if ((_BOARD_TYPE_ & _6681_BOARD_) == _6681_BOARD_)
  if (CheckIfTheIndicatorLightIsOn() == true)
    return true;
#endif

  return false;
}

bool RfidGetDeviceWakeupStatus(void)
{
  return EventGetDeviceWakeupFlagBit();
}

void RfidPlayStartSelfTestSound(void)
{
//  PromptUserKeyedKeys();
}

void RfidPlayExitSelfTestSound(void)
{
//  PromptExitKeyEntry();
}

void RfidPlaySettingStatusAudio(bool Status)
{
  EventHintParameterSetStatus(Status);
}

bool RfidGetGuestCardEn(void)
{
  if (GetGuestCardEn() == true)
    return true;

  return false;
}

bool RfidGetMasterCardEn(void)
{
  if (GetMasterCardEn() == true)
    return true;

  return false;
}

bool RfidGetBuildCardEn(void)
{
  if (GetBuildCardEn() == true)
    return true;

  return false;
}

bool RfidGetFloorCardEn(void)
{
  if (GetFloorCardEn() == true)
    return true;

  return false;
}

bool RfidGetParkCardEn(void)
{
  if (GetParkCardEn() == true)
    return true;

  return false;
}

bool RfidGetNormalOpenCardEn(void)
{
  if (GetNormalOpenCardEn() == true)
    return true;

  return false;
}

void RfidWriteCalibrationCardNumberLog(const void *pCardNumberIn, const uint8_t NumberOfCard, const uint8_t Status)
{
  uint8_t LogStatus = CARDHOLDER_AUTHECTICATION_ERROR;

  switch (Status)
  {
  case OFFLINE_CARD_AUTHENTICATION_SUCCESS:
    LogStatus = CARDHOLDER_AUTHENTICATION_SUCCESS;
    break;

  case OFFLINE_CARD_FORBIDDEN:
    LogStatus = CARDHOLDER_FORBIDDEN;
    break;

  case OFFLINE_CARD_OUT_OF_DATE:
    LogStatus = CARDHOLDER_OUT_OF_DATE;
    break;
  }

  WriteCalibrationCardNumberLog(pCardNumberIn, RFID_NUMBER_OF_CARD_SERIAL_NUMBER, LogStatus);
}

void RfidWriteGuestCardNumberLog(const void *pCardNumberIn, const uint8_t NumberOfCard, const uint8_t Status)
{
  uint8_t LogStatus = CARDHOLDER_AUTHECTICATION_ERROR;

  switch (Status)
  {
  case OFFLINE_CARD_AUTHENTICATION_SUCCESS:
    LogStatus = CARDHOLDER_AUTHENTICATION_SUCCESS;
    break;

  case OFFLINE_CARD_FORBIDDEN:
    LogStatus = CARDHOLDER_FORBIDDEN;
    break;

  case OFFLINE_CARD_OUT_OF_DATE:
    LogStatus = CARDHOLDER_OUT_OF_DATE;
    break;
  }

  WriteGuestCardNumberLog(pCardNumberIn, RFID_NUMBER_OF_CARD_SERIAL_NUMBER, LogStatus);
}

void RfidWriteMasterCardNumberLog(const void *pCardNumberIn, const uint8_t NumberOfCard, const uint8_t Status)
{
  uint8_t LogStatus = CARDHOLDER_AUTHECTICATION_ERROR;

  switch (Status)
  {
  case OFFLINE_CARD_AUTHENTICATION_SUCCESS:
    LogStatus = CARDHOLDER_AUTHENTICATION_SUCCESS;
    break;

  case OFFLINE_CARD_FORBIDDEN:
    LogStatus = CARDHOLDER_FORBIDDEN;
    break;

  case OFFLINE_CARD_OUT_OF_DATE:
    LogStatus = CARDHOLDER_OUT_OF_DATE;
    break;
  }

  WriteMasterCardNumberLog(pCardNumberIn, RFID_NUMBER_OF_CARD_SERIAL_NUMBER, LogStatus);
}

void RfidWriteBuildCardNumberLog(const void *pCardNumberIn, const uint8_t NumberOfCard, const uint8_t Status)
{
  uint8_t LogStatus = CARDHOLDER_AUTHECTICATION_ERROR;

  switch (Status)
  {
  case OFFLINE_CARD_AUTHENTICATION_SUCCESS:
    LogStatus = CARDHOLDER_AUTHENTICATION_SUCCESS;
    break;

  case OFFLINE_CARD_FORBIDDEN:
    LogStatus = CARDHOLDER_FORBIDDEN;
    break;

  case OFFLINE_CARD_OUT_OF_DATE:
    LogStatus = CARDHOLDER_OUT_OF_DATE;
    break;
  }

  WriteBuildCardNumberLog(pCardNumberIn, RFID_NUMBER_OF_CARD_SERIAL_NUMBER, LogStatus);
}

void RfidWriteFloorCardNumberLog(const void *pCardNumberIn, const uint8_t NumberOfCard, const uint8_t Status)
{
  uint8_t LogStatus = CARDHOLDER_AUTHECTICATION_ERROR;

  switch (Status)
  {
  case OFFLINE_CARD_AUTHENTICATION_SUCCESS:
    LogStatus = CARDHOLDER_AUTHENTICATION_SUCCESS;
    break;

  case OFFLINE_CARD_FORBIDDEN:
    LogStatus = CARDHOLDER_FORBIDDEN;
    break;

  case OFFLINE_CARD_OUT_OF_DATE:
    LogStatus = CARDHOLDER_OUT_OF_DATE;
    break;
  }

  WriteFloorCardNumberLog(pCardNumberIn, RFID_NUMBER_OF_CARD_SERIAL_NUMBER, LogStatus);
}

void RfidWriteParkCardNumberLog(const void *pCardNumberIn, const uint8_t NumberOfCard, const uint8_t Status)
{
  uint8_t LogStatus = CARDHOLDER_AUTHECTICATION_ERROR;

  switch (Status)
  {
  case OFFLINE_CARD_AUTHENTICATION_SUCCESS:
    LogStatus = CARDHOLDER_AUTHENTICATION_SUCCESS;
    break;

  case OFFLINE_CARD_FORBIDDEN:
    LogStatus = CARDHOLDER_FORBIDDEN;
    break;

  case OFFLINE_CARD_OUT_OF_DATE:
    LogStatus = CARDHOLDER_OUT_OF_DATE;
    break;
  }

  WriteParkCardNumberLog(pCardNumberIn, RFID_NUMBER_OF_CARD_SERIAL_NUMBER, LogStatus);
}

void RfidWriteClearCardNumberLog(const void *pCardNumberIn, const uint8_t NumberOfCard, const uint8_t Status)
{
  uint8_t LogStatus = CARDHOLDER_AUTHECTICATION_ERROR;

  switch (Status)
  {
  case OFFLINE_CARD_AUTHENTICATION_SUCCESS:
    LogStatus = CARDHOLDER_AUTHENTICATION_SUCCESS;
    break;

  case OFFLINE_CARD_FORBIDDEN:
    LogStatus = CARDHOLDER_FORBIDDEN;
    break;

  case OFFLINE_CARD_OUT_OF_DATE:
    LogStatus = CARDHOLDER_OUT_OF_DATE;
    break;
  }

  WriteCleaningStaffCardNumberLog(pCardNumberIn, RFID_NUMBER_OF_CARD_SERIAL_NUMBER, LogStatus);
}

void RfidWriteClearParameterCardNumberLog(const void *pCardNumberIn, const uint8_t NumberOfCard, const uint8_t Status)
{
  uint8_t LogStatus = CARDHOLDER_AUTHECTICATION_ERROR;

  switch (Status)
  {
  case OFFLINE_CARD_AUTHENTICATION_SUCCESS:
    LogStatus = CARDHOLDER_AUTHENTICATION_SUCCESS;
    break;
  }

  WriteClearTheKeyLog(pCardNumberIn, RFID_NUMBER_OF_CARD_SERIAL_NUMBER, LogStatus);
}

void RfidWriteInstallCardNumberLog(const void *pCardNumberIn, const uint8_t NumberOfCard, const uint8_t Status)
{
}

bool RfidGetDeviceBusyEn(void)
{
  if (EventGetVerifyNormalOpenPermissionTaskStartEn() == true)
    return true;

  if (EventGetLocalRegisterKeyStartEn() == true)
    return true;

  return false;
}

void RfidPreparationsBeforeLocalRegister(void)
{
//  PlayAddNewCardAudio();

  EventSetLocalRegisterKeyStep(LOCAL_REGISTER_PLACE_THE_CARD);
  EventUpdateLocalRegisterKeyResult(LOCAL_REGISTER_RESULT_BUSY);
}

void RfidHintAntiLockHandler(void)
{
  EventDeviceAutiLock();
}

void RfidHintCardRepaceErrorHandler(void)
{
  EventCardReplace();
}

void RfidHintCardOutOfDateHanlder(void)
{
  EventCardholderOutOfDate();
}

void RfidHintAuthenticationErrorHanlder(void)
{
  EventCardholderAuthenticationError();
}

void RfidHintAuthenticationSuccessHanlder(uint8_t CardType, const uint8_t *pCardSerialNumberIn, bool NormalOpenEn)
{
  if (pCardSerialNumberIn != NULL)
  {
    uint8_t ConvertedUnlockingType = 0, UnlockType = 0;

    switch (CardType)
    {
    case RFID_GUEST_ROOM_CARD:
      ConvertedUnlockingType = LOG_GUEST_ROOM_CARD;
      break;

    case RFID_MASTER_ROOM_CARD:
      ConvertedUnlockingType = LOG_MASTER_ROOM_CARD;
      break;

    case RFID_BUILDING_CARD:
      ConvertedUnlockingType = LOG_BUILD_ROOM_CARD;
      break;

    case RFID_FLOOR_CARD:
      ConvertedUnlockingType = LOG_FLOOR_ROOM_CARD;
      break;

    case RFID_PARK_CARD:
      ConvertedUnlockingType = LOG_PARK_ROOM_CARD;
      break;

    default:
      ConvertedUnlockingType = PROTOCOL_DUMMY_CODING;
      break;
    }
#if 0
    if (NormalOpenEn != true)
    {
      UnlockType = (CardType != RFID_NORMAL_OPEN_CARD) ? DOOR_ACTION_UNLOCK : DOOR_ACTION_LOCK;
    }
    else
    {
      UnlockType = (RfidGetNormalOpenEn() == true) ? DOOR_ACTION_NORMAL_OPEN : DOOR_ACTION_UNLOCK;
    }
#endif
    EventOpenRealy();
//    EventExecuteUnlockTask(ConvertedUnlockingType, pCardSerialNumberIn, 0, UnlockType);
  }
}

bool RfidGetDoubleLockEn(void)
{
//  return GetAntiLockSignalTiggerEn();
}

bool RfidGetWaitUserActionEn(void)
{
  if (EventGetActionTriggerStatus(DEV_LOCAL_REGISTER) == true)
    return true;

  if (EventGetVerifyNormalOpenPermissionTaskStartEn() == true)
    return true;

  return false;
}

uint32_t RfidGetTimestamp(void)
{
  return RealTimeMakeCurrentTimestamp();
}

void RfidGetTheCurrentTime(uint16_t *pYr, uint8_t *pMt, uint8_t *pDe, uint8_t *pHr, uint8_t *pMe, uint8_t *pSd, uint8_t *pWk)
{
   RealTimeRtcGetCurrentTime(pYr, pMt, pDe, pHr, pMe, pSd, pWk);
}

bool RfidUpdateRealTime(uint32_t CurrectTime)
{
  return RealTimeTimestampToRtc(CurrectTime);
}

void RfidStartDeviceFactorySetting(void)
{
  EventStartFactorySettingTask();
}

void RfidDeleteAllCardholderInformation(void)
{
  InitialiseCardholderDirectory();
  MemoryDeleteAllUserFromExternalModule();
}

uint16_t RfidGetRWDataLength(const uint8_t *pFirstPacketIn)
{
  uint16_t MessageDataSize = 0;

  if (pFirstPacketIn != NULL)
  {
    MessageDataSize = pFirstPacketIn[0] << 8;
    MessageDataSize |= pFirstPacketIn[1];
  }

  return MessageDataSize;
}

void RfidVerifyVirtualCardValidity(uint8_t *pCardSerialNumberIn)
{
  RfidReportedCardSerialNumber(pCardSerialNumberIn);
}

void RfidCalXorEncrypt(uint8_t *pKeyIn, uint8_t KeyLength, uint8_t *pDataPacket, uint32_t DataSize)
{
  XorEncryptCalculate(pKeyIn, KeyLength, pDataPacket, DataSize);
}

void RfidCalXorDecrypt(uint8_t *pKeyIn, uint8_t KeyLength, uint8_t *pDataPacket, uint32_t DataSize)
{
  XorDecryptCalculate(pKeyIn, KeyLength, pDataPacket, DataSize);
}

void RfidCalDesEncrypt(const uint8_t *pKeyIn, const uint8_t *pDataIn, uint8_t *pDataOut)
{
  CalDesEncrypt(pKeyIn, pDataIn, pDataOut);
}

void RfidCalDesDecrypt(const uint8_t *pKeyIn, const uint8_t *pDataIn, uint8_t *pDataOut)
{
  CalDesDecrypt(pKeyIn, pDataIn, pDataOut);
}

void RfidAesEcbEncrypt(uint8_t *pKeyIn, uint8_t *pDataIn, uint8_t *pDataOut)
{
  LL_Encrypt(pKeyIn, pDataIn, pDataOut);
}

void RfidAesEcbDecrypt(uint8_t *pKeyIn, uint8_t *pDataIn, uint8_t *pDataOut)
{
  LL_Decrypt(pKeyIn, pDataIn, pDataOut);
}

void RfidReportSelfTestResult(bool Result)
{
  EventMarkingModuleSelfTestResults(RFID_DEV_SELF_TEST, Result);
}

bool RfidReportedCardSerialNumber(uint8_t *pCardSerialNumberIn)
{
  if (pCardSerialNumberIn == NULL)
  {
    if (EventGetVerifyNormalOpenPermissionTaskStartEn() == true)
      return EventExitVerifyNormalOpenPermissionTask(KEY_TYPE_CARD, NULL, 0);

    if (EventGetLocalRegisterKeyStartEn() == true)
    {
      EventUpdateLocalRegisterKeyResult(LOCAL_REGISTER_RESULT_ERROR);
      return EventExitLocalRegisterKeyTask(KEY_TYPE_CARD, NULL, 0);
    }

    return EventExitReadCardDataTask(NULL, 0);
  }
  else
  {
    if (EventGetVerifyNormalOpenPermissionTaskStartEn() == true)
      return EventExitVerifyNormalOpenPermissionTask(KEY_TYPE_CARD, pCardSerialNumberIn, RFID_NUMBER_OF_MIFARE_CARD_SERIAL_NUMBER);

    if (EventGetLocalRegisterKeyStartEn() == true)
    {
      EventUpdateLocalRegisterKeyResult(LOCAL_REGISTER_RESULT_WAIT_SAVE);
      return EventExitLocalRegisterKeyTask(KEY_TYPE_CARD, pCardSerialNumberIn, RFID_NUMBER_OF_MIFARE_CARD_SERIAL_NUMBER);
    }

    return EventExitReadCardDataTask(pCardSerialNumberIn, RFID_NUMBER_OF_MIFARE_CARD_SERIAL_NUMBER);
  }

  return false;
}

bool RfidReportedCardNearby(void)
{
  return EventStartReadCardDataTask();
}

void RfidStopSelfTestTask(void)
{
  tmos_stop_task(RfidEventTaskId, RFID_SELF_TEST);
}

void RFidDelayedStartSelfTestTask(uint32_t DelayMs)
{
  tmos_stop_task(RfidEventTaskId, RFID_SELF_TEST);
  tmos_start_task(RfidEventTaskId, RFID_SELF_TEST, DelayMs);
}

void RfidStartSelfTestTaskNoWait(void)
{
  tmos_stop_task(RfidEventTaskId, RFID_SELF_TEST);
  tmos_set_event(RfidEventTaskId, RFID_SELF_TEST);
}

void RfidStopReadCardDataTask(void)
{
  tmos_stop_task(RfidEventTaskId, RFID_READ_CARD_DATA);
}

void RFidDelayedStartReadCardDataTask(uint32_t DelayMs)
{
  tmos_stop_task(RfidEventTaskId, RFID_READ_CARD_DATA);
  tmos_start_task(RfidEventTaskId, RFID_READ_CARD_DATA, DelayMs);
}

void RfidStartReadCardDataTaskNoWait(void)
{
  tmos_stop_task(RfidEventTaskId, RFID_READ_CARD_DATA);
  tmos_set_event(RfidEventTaskId, RFID_READ_CARD_DATA);
}

void RfidBoardInitialisation(RfidExternFunctionTable_t *pExternalInterfaces)
{
  RfidNssConfiguration();
  RfidSckConfiguration();
  RfidMisoConfiguration();
  RfidMosiConfiguration();
  RfidResetConfiguration();

#if _CARD_DETECTION_TYPE_ == _CARD_DETECTION_ADC_
  RfidConfiguringDetectionPinIoFunctions();
  EventAddNewTimedTask(&CardDetectionNode, RFID_DEFAULT_CARD_DETECTION_INTERVAL_TIME, TIMED_TASK_HIGH_LEVEL, pExternalInterfaces->CardDetectionTask, false);

#else
  RfidConfigurationDetectionPinIrqnFunctions();
  AddIrqNewTable(&RfidIrqContorlNode, RfidTiggerIrqnCallback);
  EventAddNewEventHandler(&RfidIrqEventHandlerNode, RfidEventHandlerCallback);
#endif

  RfidExternalInterfaces.SelfTestTask = pExternalInterfaces->SelfTestTask;
  RfidExternalInterfaces.ReadCardDataTask = pExternalInterfaces->ReadCardDataTask;
  RfidExternalInterfaces.CardDetectionTask = pExternalInterfaces->CardDetectionTask;
  PRINT("[rfid] : RfidEventTaskId = %d\r\n", RfidEventTaskId);
  RfidEventTaskId = TMOS_ProcessEventRegister(RfidEventTaskCallback);
}

#endif
